//
//  ViewController.m
//  WebViewDemo
//
//  Created by CodeYY on 2020/1/19.
//  Copyright © 2020 CodeYY. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <pop/POPBasicAnimation.h>
#define AnimationDuration 10 // 默认动画时间
@interface ViewController ()<WKNavigationDelegate>
@property(nonatomic, weak)WKWebView* myWebView;
@property(nonatomic, assign)CGFloat scrollHeight; // 记录要动画的高度
@property(nonatomic, strong)POPBasicAnimation *basicAnimation;

// 记录动画操作节点
@property(nonatomic, strong)NSNumber *startValue;
@property(nonatomic, strong)NSNumber *endValue;
@property(nonatomic, assign)CGFloat durationTime;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   WKWebView * webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:webView];
    _myWebView = webView;
    _myWebView.navigationDelegate = self;
    _myWebView.userInteractionEnabled = NO;

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://47.101.177.48:50861/fulin_wap/index/showRegister.do"]];
    [_myWebView loadRequest:request];

    // Do any additional setup after loading the view.
}

//把动画封装在方法里面，方便进行递归调用；
-(void)performAnimation
{
    [self.myWebView.layer pop_removeAllAnimations];
    
    self.basicAnimation.fromValue = self.startValue;
    self.basicAnimation.toValue = self.endValue;
    self.basicAnimation.duration = self.durationTime;
    [self.myWebView.layer pop_addAnimation:self.basicAnimation forKey:@"myViewAnimation"];
}

- (POPBasicAnimation *)basicAnimation
{
    if (!_basicAnimation) {
        _basicAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerTranslationY];
        __weak typeof(self) weakSelf = self;
        _basicAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
            if (finished) {
                if ([self.basicAnimation.toValue floatValue] == weakSelf.scrollHeight) {
                    weakSelf.startValue = @(weakSelf.scrollHeight);
                    weakSelf.endValue =  @(0);
                }else{
                    weakSelf.startValue = @(0);
                    weakSelf.endValue = @(weakSelf.scrollHeight);
                }
                weakSelf.durationTime = AnimationDuration;
                [weakSelf performAnimation];  //当动画结束后又递归调用
            }
        };
        
    }
    return _basicAnimation;
}

#pragma mark -- WKNavigationDelegate ：主要处理一些跳转、加载处理操作

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"页面开始加载时调用");
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"页面加载失败时调用");

}

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"当内容开始返回时调用");

}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"页面加载完成之后调用");
    //获取网页的高度
    __weak typeof(self) weakSelf = self;
    [webView evaluateJavaScript:@"document.body.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){
        CGFloat contentHeight = [result floatValue];
        weakSelf.myWebView.frame = CGRectMake(0, 0, self.view.frame.size.width, contentHeight);
        weakSelf.scrollHeight = - contentHeight + weakSelf.view.frame.size.height;
        // 开启动画效果
        self.startValue = @(0);
        self.endValue=  @(weakSelf.scrollHeight);
        self.durationTime = AnimationDuration;
        [weakSelf performAnimation];
        
    }];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"提交发生错误时调用");
}

// 暂停播放处理
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if (self.basicAnimation.isPaused) {
        [self performAnimation];
    }else{
        self.basicAnimation.paused = YES;
        // 记录暂停位置
        self.startValue = @(self.myWebView.layer.frame.origin.y); //从当前停止的位置开始
        self.durationTime = AnimationDuration * fabs((self.endValue.floatValue - self.myWebView.layer.frame.origin.y) / _scrollHeight); // 剩余动画时间
    }
}
@end
